/* $RCSFile: PageKeyPhrase.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/ADNCRM.Core/UI/WebControls/PageKeyPhrase.cs $
 * $log$
 * Revision 6 2010/08/06 19:56:30  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;
using System.Web;
using System.Web.UI.WebControls;

using AddonNice.Configuration;
using AddonNice.Diagnostics;
using System.Diagnostics;
using System.IO;
using System.Web.UI;
using System.Text.RegularExpressions;


namespace AddonNice.UI.WebControls
{
	/// <summary>
	/// This control is used by the PageKeyPhrase Module. Derived from Label
	/// </summary>
	public class PageKeyPhrase : Label
	{
        public PageKeyPhrase()
        {
        }

        public string NewTextStyle
        {
            get
            {
                object o    = ViewState["NewTextStyle"];
                if ( o != null )
                    return (string)o;
                return string.Empty;
            }
            set
            {
                ViewState["NewTextStyle"] = value;
            }
        }

            
		string _KeyPhrase                       =   string.Empty;
        /// <summary>
		/// Stores current Page Key Phrase
		/// </summary>
		public string KeyPhrase
		{
			get
			{
                if ( DesignMode )
                    return StStr.PageKeyPhrase;
				
                if ( !string.IsNullOrEmpty(_KeyPhrase) )
					return _KeyPhrase;
		        PortalSettings portalSettings   =   (PortalSettings) HttpContext.Current.Items[StStr.strPortalSettings];
                CultureInfo ci                  =   portalSettings.PortalContentLanguage;
			    string currentLanguage          =   StStr.PageKeyPhrase_under+ ci.Name.ToUpper().Replace('-', '_');
			    string currentNLanguage         =   string.Empty;
                bool IsNeutralCulture           =   ci.IsNeutralCulture;
                if ( !IsNeutralCulture )
                {
                    ci                          =   ci.Parent;
		            currentNLanguage            =   StStr.PageKeyPhrase_under+ ci.Name.ToUpper().Replace('-', '_');
                }

                if ( portalSettings.ActivePageSettings.Settings.ContainsKeyNotEmpty(currentLanguage))
                    _KeyPhrase                  =   portalSettings.ActivePageSettings.Settings[currentLanguage].Value;
                else if ( !IsNeutralCulture &&  portalSettings.ActivePageSettings.Settings.ContainsKeyNotEmpty(currentNLanguage))
                    _KeyPhrase                  =   portalSettings.ActivePageSettings.Settings[currentNLanguage].Value;
                else if ( portalSettings.ActivePageSettings.Settings.ContainsKeyNotEmpty(StStr.PageKeyPhrase))
					_KeyPhrase                  =   portalSettings.ActivePageSettings.Settings[StStr.PageKeyPhrase].ToString();
			    else if ( portalSettings.CustomSettings.ContainsKey(StStr.SiteSettingsPageKeyPhrase) )
				    _KeyPhrase                  =   portalSettings.CustomSettings[StStr.SiteSettingsPageKeyPhrase].ToString();						

				return _KeyPhrase;
			}
		}
		
		protected override void OnLoad(EventArgs e)
		{
			Text    =  KeyPhrase;
			DataBind();
		}

    
        protected override void Render(HtmlTextWriter writer)
        {
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine("[V]PageKeyTrace Render BEG.",WebControlsTraceSwitch.Sw.Info);
            StringWriter sw         =   new StringWriter();
            HtmlTextWriter htw      =   new HtmlTextWriter(sw);
            base.Render(htw);
            string s                =   sw.ToString();
            if ( string.IsNullOrEmpty(NewTextStyle) )
            {
                writer.Write(s);
                if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                    Trace.WriteLine("[V]PageKeyTrace Render END no style.",WebControlsTraceSwitch.Sw.Info);
                return;
            }
            string regexPattern     =   "style=(\"|\')([^(\"|\')]+)(\"|\')";
            Regex re                =   new Regex(regexPattern, RegexOptions.ExplicitCapture|RegexOptions.IgnoreCase);
            //MatchCollection mc      =   re.Matches(s);
            string result           =   re.Replace(s,string.Format("style='{0}' ",NewTextStyle));
            writer.Write(result);
            if ( WebControlsTraceSwitch.Sw.TraceVerbose )
                Trace.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]PageKeyTrace Render END result: [{0}].",result),WebControlsTraceSwitch.Sw.Info);
        }
    }
}
